﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

namespace yule.Data
{
    /// <summary>
    /// 执行操作(Insert delete等)
    /// </summary>
    public interface IExecuteNonQuery
    {
        #region ExecuteNonQuery

        /// <summary>
        /// /// 执行语句
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="action">成功回调</param>
        /// <returns></returns>
        int ExecuteNonQuery(string cmdText, Action<string> action = null);

        /// <summary>
        /// 执行语句
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="commandParameters"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        int ExecuteNonQuery(string cmdText, IList<DbParameter> commandParameters, Action<string, IList<DbParameter>> action = null);

        /// <summary>
        /// 执行语句
        /// </summary>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        /// <param name="commandParameters"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        int ExecuteNonQuery(CommandType cmdType, string cmdText, IList<DbParameter> commandParameters, Action<string, IList<DbParameter>> action = null);


        /// <summary>
        /// 创建参数
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="value"></param>
        /// <param name="dbType"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        DbParameter CreateDbParameter(string parameterName, object value, DbType dbType, ParameterDirection direction = ParameterDirection.Input);


        #endregion ExecuteNonQuery
    }

    /// <summary>
    /// 执行事务接口
    /// </summary>
    public interface ITransaction
    {
        #region Transaction

        /// <summary>
        /// 执行事务
        /// </summary>
        /// <param name="trans"></param>
        /// <param name="cmdText"></param>
        /// <returns></returns>
        int ExecuteNonQuery(DbTransaction trans, string cmdText);

        /// <summary>
        /// 执行事务
        /// </summary>
        /// <param name="trans"></param>
        /// <param name="cmdText"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        int ExecuteNonQuery(DbTransaction trans, string cmdText, IList<DbParameter> commandParameters);

        /// <summary>
        /// 执行事务
        /// </summary>
        /// <param name="trans">事务</param>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        int ExecuteNonQuery(DbTransaction trans, CommandType cmdType, string cmdText, IList<DbParameter> commandParameters);


        /// <summary>
        /// 创建参数
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="value"></param>
        /// <param name="dbType"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        DbParameter CreateDbParameter(string parameterName, object value, DbType dbType, ParameterDirection direction = ParameterDirection.Input);


        #endregion
    }

    /// <summary>
    /// 执行查询接口
    /// </summary>
    public interface IExecuteQuery
    {

        #region ExecuteDataTable


        /// <summary>
        /// 执行查询返回DataTable
        /// </summary>
        /// <param name="cmdText"></param>
        /// <returns></returns>
        DataTable ExecuteDataTable(string cmdText);


        /// <summary>
        /// 执行查询返回DataTable
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        DataTable ExecuteDataTable(string cmdText, IList<DbParameter> commandParameters);

        /// <summary>
        /// 执行查询返回DataTable
        /// </summary>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        DataTable ExecuteDataTable(CommandType cmdType, string cmdText, IList<DbParameter> commandParameters);

        #endregion

        #region ExecuteScalar

        /// <summary>
        /// 执行查询返回,第一行第一列
        /// </summary>
        /// <param name="cmdText"></param>
        /// <returns></returns>
        object ExecuteScalar(string cmdText);
        /// <summary>
        /// 执行查询返回,第一行第一列
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        object ExecuteScalar(string cmdText, IList<DbParameter> commandParameters);
        /// <summary>
        /// 执行查询返回,第一行第一列
        /// </summary>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        object ExecuteScalar(CommandType cmdType, string cmdText, IList<DbParameter> commandParameters);
        #endregion



        /// <summary>
        /// 创建参数
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="value"></param>
        /// <param name="dbType"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        DbParameter CreateDbParameter(string parameterName, object value, DbType dbType, ParameterDirection direction = ParameterDirection.Input);


    }

    /// <summary>
    /// 数据库访问接口
    /// </summary>
    public interface IDBHelper : IExecuteQuery, IExecuteNonQuery, ITransaction
    {
        Transaction CreateTransaction();
    }
}